package com.testing.login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Classname ${NAME}
 * @Description 类型说明
 * @Date 2021/5/24 21:10
 * @Created by 特斯汀Roy
 */
public class Logout extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String result = "{";
        String status = "1";
        String msg = "恭喜您登出成功";
        //如果没有记录下来user，说明之前没有人登录过。
        if(session.getAttribute("user")==null) {
            session.invalidate();
            msg="没有人登录过，但是也帮你重置一下";
        }else {
            //如果有user,获取这个user
            String user=session.getAttribute("user").toString();
            session.invalidate();
            msg=user+"用户退出登录成功。";
        }
        result += "\"status\":\"" + status + "\"," + "\"msg\":\"" + msg + "\"}";
        response.getWriter().append(result);

    }
}
